<template>
  <div class="register-container">
    <!-- 注册卡片 -->
    <div class="register-card">
      <div class="register-header">
        <div class="logo">
          <i class="el-icon-s-platform"></i>
        </div>
        <h1 class="title">创建账户</h1>
        <p class="subtitle">加入我们的管理系统</p>
      </div>
      
      <el-form 
        :model="registerForm" 
        :rules="registerRules" 
        ref="registerFormRef" 
        class="register-form"
        @keyup.enter="handleRegister"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="registerForm.username" 
            placeholder="请输入用户名"
            size="large"
            class="register-input"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="email">
          <el-input 
            v-model="registerForm.email" 
            placeholder="请输入邮箱"
            size="large"
            class="register-input"
          >
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="registerForm.password" 
            type="password" 
            placeholder="请输入密码"
            size="large"
            class="register-input"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input 
            v-model="registerForm.confirmPassword" 
            type="password" 
            placeholder="请确认密码"
            size="large"
            class="register-input"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        
        <div class="form-options">
          <el-checkbox v-model="agreeTerms">我已阅读并同意</el-checkbox>
          <a href="#" class="terms-link">服务条款</a>
        </div>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleRegister" 
            class="register-button"
            size="large"
            :loading="loading"
          >
            {{ loading ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
        
        <div class="login-link">
          <span>已有账户？</span>
          <router-link to="/" class="link">立即登录</router-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import { User, Message, Lock } from '@element-plus/icons-vue';

export default {
  setup() {
    const router = useRouter();
    const registerForm = ref({
      username: '',
      password: '',
      confirmPassword: '',
      email: '',
    });
    
    const agreeTerms = ref(false);
    const loading = ref(false);

    const registerRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, message: '用户名至少3个字符', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur' },
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '邮箱格式不正确', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码至少6个字符', trigger: 'blur' },
        { pattern: /^(?=.*[a-zA-Z])(?=.*\d).+$/, message: '密码必须包含字母和数字', trigger: 'blur' },
      ],
      confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        { validator: (rule, value, callback) => {
          if (value !== registerForm.value.password) {
            callback(new Error('两次密码输入不一致'));
          } else {
            callback();
          }
        }, trigger: 'blur' },
      ],
    };

    const registerFormRef = ref(null);

    const handleRegister = async () => {
      if (!registerFormRef.value) return;
      
      if (!agreeTerms.value) {
        ElMessage.warning('请先同意服务条款');
        return;
      }
      
      try {
        const valid = await registerFormRef.value.validate();
        if (valid) {
          loading.value = true;
          
          // 模拟注册请求
          setTimeout(() => {
            ElMessage.success('注册成功！');
            loading.value = false;
            // 跳转到登录页面
            router.push('/');
          }, 1500);
        }
      } catch (error) {
        ElMessage.error('请检查输入信息');
        loading.value = false;
      }
    };

    return {
      registerForm,
      registerRules,
      registerFormRef,
      agreeTerms,
      loading,
      handleRegister,
    };
  },
};
</script>

<style scoped>
.register-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow-y: auto;
  z-index: 9999;
}

/* 禁用滚动条 */
.register-container::-webkit-scrollbar {
  display: none;
}
.register-container {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

:global(body) {
  overflow: hidden !important;
  background: #fff !important;
}

.register-card {
  background: transparent;
  border-radius: 0;
  padding: 60px 0 40px 0;
  width: 100%;
  max-width: 420px;
  box-shadow: none;
  border: none;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.register-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  width: 80px;
  height: 80px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 30px;
  font-size: 32px;
  color: white;
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.3);
}

.title {
  font-size: 36px;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 12px;
}

.subtitle {
  color: #7f8c8d;
  font-size: 18px;
  margin: 0;
}

.register-form {
  margin-top: 40px;
  width: 100%;
  max-width: 420px;
}

.register-input {
  margin-bottom: 25px;
}

.register-input :deep(.el-input__wrapper) {
  border-radius: 12px;
  border: 2px solid #e1e8ed;
  transition: all 0.3s ease;
  background: #f8f9fa;
  height: 55px;
}

.register-input :deep(.el-input__wrapper:hover) {
  border-color: #667eea;
  background: white;
}

.register-input :deep(.el-input__wrapper.is-focus) {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: white;
}

.form-options {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
  font-size: 16px;
}

.terms-link {
  color: #667eea;
  text-decoration: none;
  margin-left: 5px;
  transition: color 0.3s ease;
}

.terms-link:hover {
  color: #5a6fd8;
}

.register-button {
  width: 100%;
  height: 55px;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 15px 30px rgba(102, 126, 234, 0.3);
}

.register-button:active {
  transform: translateY(0);
}

.login-link {
  text-align: center;
  margin-top: 30px;
  font-size: 16px;
  color: #7f8c8d;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
  margin-left: 5px;
  transition: color 0.3s ease;
}

.link:hover {
  color: #5a6fd8;
}

@media (max-width: 1024px) {
  .register-card {
    padding: 40px 0 30px 0;
    max-width: 95vw;
  }
  .title {
    font-size: 32px;
  }
  .subtitle {
    font-size: 16px;
  }
  .logo {
    width: 70px;
    height: 70px;
    font-size: 28px;
  }
}
@media (max-width: 600px) {
  .register-card {
    padding: 20px 0 10px 0;
    max-width: 100vw;
  }
  .register-form {
    max-width: 100vw;
  }
}
@media (min-width: 1440px) {
  .register-card {
    padding: 70px 0 50px 0;
    max-width: 480px;
  }
  .title {
    font-size: 40px;
  }
  .subtitle {
    font-size: 20px;
  }
  .logo {
    width: 90px;
    height: 90px;
    font-size: 36px;
  }
}
@media (min-width: 1920px) {
  .register-card {
    padding: 80px 0 60px 0;
    max-width: 550px;
  }
  .title {
    font-size: 44px;
  }
  .subtitle {
    font-size: 22px;
  }
  .logo {
    width: 100px;
    height: 100px;
    font-size: 40px;
  }
  .register-button {
    height: 60px;
    font-size: 20px;
  }
  .register-input :deep(.el-input__wrapper) {
    height: 60px;
  }
}
</style>